package com.hinnotek.amoeba.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.SellListAdapter
import com.hinnotek.amoeba.bean.old.SysOrgDetailByIdBean
import com.hinnotek.amoeba.databinding.FragmentHomeWorkBinding
import com.hinnotek.amoeba.ui.activity.old.*
import com.hinnotek.amoeba.utilities.*
import com.hinnotek.amoeba.viewmodels.ManageViewModel
import com.lee.common.base.BaseFragment
import com.lee.common.network.net.ObserverState
import com.lee.common.utils.SharedPreUtils
import dagger.hilt.android.AndroidEntryPoint
import java.util.*
import com.lee.common.utils.*
import com.lee.common.utils.ToastUtils

/**
 * "项目规划"Fragment标签页
 */
@AndroidEntryPoint
class FragmentHomeWork : BaseFragment<FragmentHomeWorkBinding>(), View.OnClickListener {
    private val amoeba = arrayOf("销售", "技术", "研发")
    private val company = arrayOf("商务", "财务", "手机号缴费申请")
    private val amoebaList: MutableList<Bean> = ArrayList()
    private val companyList: MutableList<Bean> = ArrayList()
    private var adapter: SellListAdapter? = null
    private var adapter1: SellListAdapter? = null
    private var sysOrgId = -1

    val viewModel by viewModels<ManageViewModel>()


    override fun initData() {
        mBinding.llNoticeType1.setOnClickListener(this)
        if (SharedPreUtils.getInteger(isAmoebaOrCompany, 0) == 1) {

            mBinding.xuiAmoeba.visibility = View.VISIBLE

            mBinding.xuiCompany.visibility = View.GONE
            if (SharedPreUtils.getBoolean(isAmoebaStatistics, false)) {
                mBinding.llNoticeType1.setVisibility(View.VISIBLE)
            }
            if (SharedPreUtils.getBoolean(isAmoebaSell, false)) {
                amoebaList.add(Bean(R.drawable.sell, "销售"))
            }
            if (SharedPreUtils.getBoolean(isAmoebaTechnology, false)) {
                amoebaList.add(Bean(R.drawable.technology, "技术"))
            }
            if (SharedPreUtils.getBoolean(isAmoebaRD, false)) {
                amoebaList.add(Bean(R.drawable.technology, "研发"))
            }
            getSysOrgDetailById()
        } else {
            mBinding.xuiCompany.setVisibility(View.VISIBLE)
            mBinding.xuiAmoeba.setVisibility(View.GONE)
            if (SharedPreUtils.getBoolean(isCompanyBusiness, false)) {
                companyList.add(Bean(R.drawable.company_business, "商务"))
            }
            if (SharedPreUtils.getBoolean(isCompanyFinance, false)) {
                companyList.add(Bean(R.drawable.company_finance, "财务"))
            }
            if (SharedPreUtils.getBoolean(isPhoneNumPay, false)) {
                companyList.add(Bean(R.drawable.phone, "手机号缴费申请"))
            }
        }
        val manager = GridLayoutManager(mContext, 3)
        manager.orientation = LinearLayoutManager.VERTICAL

        mBinding.rlvAmoeba.setLayoutManager(manager)
        mBinding.rlvAmoeba.setAdapter(SellListAdapter().also { adapter = it })
        adapter?.setOnItemClickListener { adapter2, view1, position ->
            if (ClickUtil.isFastClick()) {
                return@setOnItemClickListener
            }
            val s = (adapter2.getItem(position) as Bean).tv
            when (s) {
                "销售" -> IntentUtils.startActivity(mContext, SellListActivity::class.java)
                "技术" -> IntentUtils.startActivity(mContext, TechnologyListActivity::class.java)
                "研发" -> IntentUtils.startActivity(mContext, RDListActivity::class.java)
                "付款" -> IntentUtils.startActivity(mContext, PaymentRequestListActivity::class.java)
                "报销" -> IntentUtils.startActivity(mContext, WipeOutListActivity::class.java)
                "报销金额" -> {
                }
            }
        }
        adapter?.setList(amoebaList)
        val manager1 = GridLayoutManager(mContext, 3)
        manager1.orientation = LinearLayoutManager.VERTICAL

        mBinding.rlvCompany.layoutManager = manager1
        mBinding.rlvCompany.adapter = SellListAdapter().also { adapter1 = it }
        adapter1?.setOnItemClickListener { adapter2, _, position ->
            if (ClickUtil.isFastClick()) {
                return@setOnItemClickListener
            }
            val s = (adapter2.getItem(position) as Bean).tv
            when (s) {
                "商务" -> IntentUtils.startActivity(mContext, BusinessListActivity::class.java)
                "财务" -> IntentUtils.startActivity(mContext, FinancialListActivity::class.java)
                "库房" -> {
                }
                "产品管理" -> IntentUtils.startActivity(
                    mContext,
                    ProductManagamentActivity::class.java
                )
                "付款" -> IntentUtils.startActivity(mContext, PaymentRequestListActivity::class.java)
                "报销" -> IntentUtils.startActivity(mContext, WipeOutListActivity::class.java)
                "手机号缴费申请" -> IntentUtils.startActivity(
                    mContext,
                    PhoneNumberPaymentListActivity::class.java
                )
            }
        }
        adapter1?.setList(companyList)
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_home_work
    }


    fun setFresh() {
        if (SharedPreUtils.getInteger(isAmoebaOrCompany, 0) == 1) {
            getSysOrgDetailById()
        }
    }


    override fun onResume() {
        super.onResume()
        setFresh()
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.ll_notice_type1 -> if (sysOrgId != -1) {
                val bundle = Bundle()
                bundle.putInt("orgId", sysOrgId)
                IntentUtils.startActivity(mContext, StatisticalActivity::class.java, bundle)
            }
        }
    }
    /**
     * app端获取全部阿米巴组织详情带金额接口
     */
    fun getSysOrgDetailById() {
        val orgId = SharedPreUtils.getInteger("orgId", 0)
        viewModel.getSysOrgDetailById(orgId)
            .observe(this, object : ObserverState<SysOrgDetailByIdBean>() {
                override fun onDataChange(data: SysOrgDetailByIdBean) {
                    sysOrgId = data.orgId
                    mBinding.tvAmoebaName.setText(data.orgName)
                    mBinding.tvAmountToBeCredited.setText(data.deservedTotal)
                    mBinding.tvActualAmount.setText(data.actualTotal)
                    mBinding.tvCostAmount.setText(data.costTotal)
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    ToastUtils.showShort(activity, msg)
                    super.onError(msg, code)
                }
            })
    }



    class Bean(var iv: Int, var tv: String)


}